package com.wg.dome.handle;

import com.wg.dome.ClientPeer;
import com.wg.dome.ErrorCode;
import com.wg.dome.ErrorHelper;
import com.wg.dome.annotation.Request;
import com.wg.dome.entity.config.ZhuLingRingEntity;
import com.wg.dome.entity.user.EquipEntity;
import com.wg.dome.entity.user.ItemEntity;
import com.wg.dome.operation.OperationCode;
import com.wg.dome.operation.ZhuLingRequest;
import com.wg.dome.operation.EquipZhuLingResponse;
import com.wg.dome.repository.*;
import com.wg.common.net.OperationRequest;
import com.wg.common.net.OperationResponse;
import com.wg.common.net.SendParameters;

/**
 * 注灵装备
 */
@Request(OperationCode = OperationCode.ZHU_LING)
public class HandleZhuLingRequest extends BaseHandle {

    @Override
    public OperationResponse execute(ClientPeer peer, OperationRequest request, SendParameters sendParameters) {

        ZhuLingRequest zhuLingRequest = new ZhuLingRequest(request);

        if (!zhuLingRequest.isValid()) {
            return ErrorHelper.getInvalidResponse(zhuLingRequest);
        }

        if (zhuLingRequest.getPlayerId() != peer.getPlayerId()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.PLAYERID_MATCH_ERROR, zhuLingRequest.getPlayerId(), peer.getPlayerId());
        }

        EquipRepository equipRepository = new EquipRepository();
        EquipEntity equipEntity = equipRepository.getEntity(zhuLingRequest.getPlayerId(), zhuLingRequest.getEquipId());

        if (equipEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.EQUIP_ERROR, zhuLingRequest.getEquipId());
        }

        short zlLv = equipEntity.getZlLv();

        ZhuLingRingRepository zhuLingRingRepository = new ZhuLingRingRepository();
        ZhuLingRingEntity entity = zhuLingRingRepository.getNextEntity(zlLv);
        if (entity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.LEVEL_MAX);
        }

        ItemRepository itemRepository = new ItemRepository();
        ItemEntity itemEntity = itemRepository.getItem(zhuLingRequest.getPlayerId(), entity.getUseItemId());
        if (itemEntity == null) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.ITEM_LIMIT, entity.getUseItemId());
        }

        if (entity.getUseItemCount() > itemEntity.getCount()) {
            return ErrorHelper.getErrorResponse(request.getOperationCode(),
                    ErrorCode.ITEM_LIMIT);
        }

        int willUseCount = 0;
        int useCount = 0;


        for (short i = equipEntity.getZlLv(); i < equipEntity.getZlLv() + 5; i++) {
            ZhuLingRingEntity zhuLingRingEntity = zhuLingRingRepository.getNextEntity(i);
            if (zhuLingRingEntity == null) {
                break;
            }
            willUseCount += zhuLingRingEntity.getUseItemCount();
            if (willUseCount > itemEntity.getCount()) {
                break;
            }
            useCount += zhuLingRingEntity.getUseItemCount();
            zlLv++;
        }

        equipEntity.setZlLv(zlLv);
        equipRepository.update(equipEntity);

        itemEntity.setCount(itemEntity.getCount() - useCount);
        itemRepository.update(itemEntity);

        EquipZhuLingResponse response = new EquipZhuLingResponse();
        response.setEquipId(zhuLingRequest.getEquipId());
        response.setCount(itemEntity.getCount());
        response.setLevel(zlLv);
        response.setItemId(itemEntity.getsId());

        return new OperationResponse(request.getOperationCode(), response);
    }
}
